package controller

import (
	"ahutoj/web/io/constanct"
	"ahutoj/web/io/request"
	"ahutoj/web/io/response"
	"ahutoj/web/logic"
	"ahutoj/web/utils"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// @Summary	添加题目
// @Description
// @Tags		Problem
// @Accept		json
// @Produce	json
// @Param		Problem	body		request.Problem	true	"-"
// @Success	200		{object}	response.AddProblemResp
// @Router		/api/problem/add/ [post]
func AddProblem(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.Problem)
	err := ctx.ShouldBindWith(req, binding.JSON)
	if err != nil {
		// 请求参数有误 直接返回响应
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	// fmt.Printf("req:%+v\n", req)
	resp, err := logic.AddProblem(ctx, req)
	if err != nil {
		logger.Errorf("call AddProblem failed,req=%+v,err=%s", *req, err.Error())
		response.ResponseError(ctx, resp.(constanct.ResCode))
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取题库列表
// @Description LOCALTYPE  ProblemType = "LOCAL" CODEFORCESTYPE ProblemType = "CODEFORCES" ATCODERTYPE ProblemType = "ATCODERTYPE"
// @Tags		Problem
// @Accept		json
// @Produce	json
// @Param		ProblemListResp	query		request.ProblemListReq	true	"-"
// @Success	200				{object}	response.ProblemListResp
// @Router		/api/problem/list [get]
func GetProblemList(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.ProblemListReq)
	err := ctx.ShouldBindWith(req, binding.Query)
	if err != nil {
		// 请求参数有误 直接返回响应
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.GetProblemList(ctx, req)
	if err != nil {
		logger.Errorf("call GetProblemList failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取题目详情
// @Description
// @Tags		Problem
// @Accept		json
// @Produce	json
// @Param		id	path		string	true	"PID"
// @Success	200	{object}	response.Response
// @Router		/api/problem/{id} [get]
func GetProblem(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	PID := ctx.Param("id")
	resp, err := logic.GetProblemInfo(ctx, PID)
	if err != nil {
		logger.Errorf("call GetProblemInfo failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	编辑题目
// @Description
// @Tags		Problem
// @Accept		json
// @Produce	json
// @Param		EditProblemReq	body		request.EditProblemReq	true	"-"
// @Success	200				{object}	response.Response
// @Router		/api/problem/edit/ [post]
func EditProblem(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.EditProblemReq)
	err := ctx.ShouldBindWith(req, binding.JSON)
	if err != nil {
		//请求参数有误 直接返回响应
		logger.Errorf("call ShouldBindWith failed, err =%s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.EditProblem(req, ctx)
	if err != nil {
		logger.Errorf("call EditProblem failed,req=%+v,err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	删除题目
// @Description
// @Tags		Problem
// @Accept		json
// @Produce	json
// @Param		DeleteProblemReq	body		request.DeleteProblemReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/problem/delete/ [post]
func DeleteProblem(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.DeleteProblemReq)
	err := ctx.BindJSON(req)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err =%s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.DeleteProblem(ctx, req)
	if err != nil {
		logger.Errorf("call DeleteProblem failed,req=%+v,err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}
